package net.lzzy.spingsecurity.service;

import net.lzzy.spingsecurity.entity.Article;
import net.lzzy.spingsecurity.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public class ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    //分页
    public Page<Article> pageArticle(int page, int size){
        Pageable pageable = PageRequest.of(page, size);
        return articleRepository.findAll(pageable);
    }

    public Article findArticleById(Integer id){
        if(articleRepository.findById(id).isPresent()){
            return articleRepository.findById(id).get();
        }
        else{
            return null;
        }
    }
}
